// pages/orderDetail/orderDetail.js
import Common from "../../utils/common.js"
import {
  Get,
  Post,
  JsonPost
} from "../../utils/request.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    iconUrl: '',
    baseInfo: {},
    success: true,
    paying: false,
    order_id:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      order_id:options.id
    })
    this.getDetail()
  },

  getDetail(){
    JsonPost('order/detail',{'order_id':this.data.order_id}).then(res=>{
      let obj=res.data
      obj.addtime=Common.formatTimestamp(obj.addtime * 1000, 'yyyy/MM/dd')
      obj.pay_time=Common.formatTimestamp(obj.pay_time * 1000, 'yyyy/MM/dd')
      obj.ship_time=Common.formatTimestamp(obj.ship_time * 1000, 'yyyy/MM/dd')
      obj.complete_time=Common.formatTimestamp(obj.complete_time * 1000, 'yyyy/MM/dd')
      obj.consignee=obj.distribute_type==3?'收货人: '+obj.consignee:'提货点: '+obj.consignee
      this.setData({
        baseInfo:obj
      })
    })
  },

  
  // 拨打手机号
  copyPhone() {
    wx.makePhoneCall({
      phoneNumber: this.data.baseInfo.mobile
    })
  },


  //评论id
  goPinLun(e) {
    wx.navigateTo({
      url: '/pages/evaluate/evaluate?row=' + e.currentTarget.dataset.row
    })
  },


  //再次购买
  tooBuy(e) {
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?goodsId=' + e.currentTarget.dataset.id
    })
  },

  //删除订单
  delOrder(e) {
    let that=this
    wx.showModal({
      title: '提示',
      content: '确认删除?',
      success(res) {
        if (res.confirm) {
          JsonPost('order/cancel',{'order_id':e.currentTarget.dataset.id }).then(resp => {
            that.setData({
              list:[]
            })
            that.getList()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //关闭订单
  closeOrder(e) {
    let that=this
    wx.showModal({
      title: '提示',
      content: '确认取消该订单?',
      success(res) {
        if (res.confirm) {
          JsonPost('order/cancel',{'order_id':e.currentTarget.dataset.id }).then(resp => {
            that.setData({
              list:[]
            })
            that.getList()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //确认收货
  SHed(e) {
    let that=this
    wx.showModal({
      title: '提示',
      content: '确认收货?',
      success(res) {
        if (res.confirm) {
          JsonPost('order/confirm-goods',{'order_id':e.currentTarget.dataset.id }).then(resp => {
            that.setData({
              list:[]
            })
            that.getList()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //申请售后
  goSouhou(e) {
    wx.navigateTo({
      url: '/pages/submitSale/submitSale?row=' + JSON.stringify(e.currentTarget.dataset.row)
    })
  },

  //查看
  souDetail(e) {
    JsonPost('order/logistics',{'order_id':e.currentTarget.dataset.id}).then(log=>{
      wx.showModal({
        title: '物流信息',
        content: '物流单号: '+log.data.logistics_sn+' 物流公司:'+log.data.logistics_name,
        confirmText: '复制',
        success (res) {
          if (res.confirm) {
            wx.setClipboardData({
              data: log.data.logistics_sn,
              success(res) {
        
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    })
  },

  //付款
  pay(e) {
    let that=this
    if (this.data.paying) return
    wx.showLoading({
      title: '发起支付中',
    })
    this.setData({
      paying: true
    })
    JsonPost('payment/wxPay',{'order_sn':e.currentTarget.dataset.id}).then(Pes=>{
      wx.hideLoading()
      wx.requestPayment({
        timeStamp: Pes.data.timeStamp,
        nonceStr: Pes.data.nonceStr,
        package: Pes.data.package,
        signType: 'MD5',
        paySign: Pes.data.paySign,
        success(res) {
          console.log(res, 'success')
          that.setData({
            paying: false,
          })
          that.getList()
        },
        fail(res) {
          console.log(res, 'fail')
          that.setData({
            paying: false,
          })
          wx.showToast({
            title: '支付失败',
            icon: 'none',
            duration: 2000
          })
        }
      })
    })
    that.setData({
      paying: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})